package com.etc.controller;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.etc.entity.Moments;
import com.etc.service.impl.MomentsServiceImpl;
import com.etc.util.PageData;
import com.google.gson.Gson;


@WebServlet("/MomentsController")
public class MomentsController extends HttpServlet {
	private static final long serialVersionUID = 1L;
	MomentsServiceImpl service=new MomentsServiceImpl();
       
    public MomentsController() {
        super();
    }


	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//设置请求编码
		request.setCharacterEncoding("utf-8");
		
		//获取参数 参数名op的值如果等于query才做查询
		String op = request.getParameter("op");
		
		//提升:加入session的判断:从session中获取用户登录的信息,如果有,才能执行查询,添加等等的操作
		HttpSession session = request.getSession();
		Object object = session.getAttribute("backUser");
		
		//判断是否已经登录
		if (object != null) {
			//管理员登录后执行的操作
			adminOp(op,request,response);
			
		}else {
			//访问的时候发现没有登录,跳转到登陆页面去
			response.sendRedirect("back/login.jsp");
		}
		
	}

	/**
	 * 管理员登录之后才可以对时刻表进行的操作
	 * @param op
	 * @param request
	 * @param response
	 * @throws IOException 
	 * @throws ServletException
	 * @author zww 
	 */
	public void adminOp(String op, HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		//车辆时刻信息全查
		if("queryMents".equals(op)) {
			//调用全查方法
			List<Moments> list = service.queryMents();
			//将查询到的结果存储到request作用域中
			request.setAttribute("list", list);
			//转发
			request.getRequestDispatcher("back/moment-information.jsp").forward(request, response);
			
		}else if("addMoments".equals(op)) {
			//添加时刻
			//获取首发时间
			String m_begin = request.getParameter("mbegin");
			//获取某班时间
			String m_end = request.getParameter("mend");
			//获取发车间隔
			String m_interval = request.getParameter("minterval");
			
			Moments moments = new Moments(m_begin, m_end, m_interval);
			boolean flag = service.insertments(moments);
			
			//转发到servlet 
			request.getRequestDispatcher("MomentsController?op=queryMents").forward(request, response);
			
		}else if("momentsByPage_ajax".equals(op)) {
			//时刻信息分页查询
			momentsByPage_ajax(request,response);
			
		}else if("update_ajax".equals(op)) {
			//时刻信息修改
			update_ajax(request,response);
		
		}else if("dele_ajax".equals(op)) {
			//删除时刻信息
			dele_ajax(request,response);
		}


		
	}

	/**
	 * 删除时刻信息
	 * @param request
	 * @param response
	 * @throws IOException
	 */
	public void dele_ajax(HttpServletRequest request, HttpServletResponse response) throws IOException {
		//获取用户传递过来的参数m_id
		String m_id = request.getParameter("m_id");
		int mid = Integer.parseInt(m_id);
		//调用service
		boolean flag = service.delMoments(mid);
		//通过ajx返回json数据
		Gson gson = new Gson();
	    String jsonFlag = gson.toJson(flag);
	    response.getWriter().print(jsonFlag);
	    response.getWriter().close();
		
	}


	/**
	 * 修改时刻信息
	 * @param request
	 * @param response
	 * @throws IOException
	 * @author zww
	 */
	private void update_ajax(HttpServletRequest request, HttpServletResponse response) throws IOException {
		// 接收参数(此时ajax提交的参数是一个由js对象转换成的json格式字符串)
	    String jsonString = request.getParameter("moments");
	    //通过gson的fromjson()转成java对象
	    Gson gson = new Gson();
	    Moments moments = gson.fromJson(jsonString, Moments.class);
	    //调用业务层的修改方法
	    boolean flag = service.updateMonents(moments);
	    //通过ajx返回json数据
	    String jsonFlag = gson.toJson(flag);
	    response.getWriter().print(jsonFlag);
	    response.getWriter().close();

		
	}


	private void momentsByPage_ajax(HttpServletRequest request, HttpServletResponse response) throws IOException {
		//时刻信息分页
		//1.默认页码和显示条数
		int pageNo=1;//默认值
		int pageSize=4;//默认值
		String titleKey="";//如果没有传递,默认查所有
		
		//2.如果前台有指定值就使用前台的数据,否则按默认显示
		if(request.getParameter("pageNo")!=null) {
			pageNo=Integer.parseInt(request.getParameter("pageNo"));
		}
		if(request.getParameter("pageSize")!=null) {
			pageSize=Integer.parseInt(request.getParameter("pageSize"));
		}
		if(request.getParameter("titleKey")!=null) {
			titleKey=request.getParameter("titleKey");
		}
		
		//3.调用业务层
		PageData pageData = service.getUserByLikeAndLimit(pageNo, pageSize, titleKey);
		
		//4.通过ajax返回json数据
		Gson gson = new Gson();
		String json = gson.toJson(pageData);
		response.getWriter().print(json);
		response.getWriter().close();
		
	}


	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		doGet(request, response);
	}

}
